import {
  addExam,
  addPractice,
  delOnlineExam,
  delOfflineExam,
  delPractice,
  addOfflineExam,
  addHomework,
  delHomework,
} from "@/api/exam/addExam";
import { getCategoryld } from "@/api/exam/examPaperTopic";

let mixin = {
  data() {
    return {
      // 添加练习组件
      practiceOpen: false,
      practiceNum: 100,
      // 添加线上考核组件
      OnlineAssessmentOpen: false,
      OnlineAssessmentOpenNum: 100,
      // 添加线下考核组件
      OfflineAssessmentOpen: false,
      // 考试组件
      exam_paperOpen: false,
      exam_examId: "",
      exam_taskId: "",
      exam_type: 1,
      //   题库组件
      question_id: "",
      question_open: false,
    };
  },
  created() {},
  methods: {
    /* 题库按钮 */
    handleQuestionBox(taskId) {
      getCategoryld(taskId).then((res) => {
        this.question_id = res.data;
        this.question_open = true;
        this.$nextTick(() => {
          this.$refs.myTopicList.getList();
        });
      });
    },
    /* 添加线上考核 */
    handleAddExam(exam, taskId, type) {
      let { examPublish, examId } = exam;
      localStorage.setItem("uuid", this.courseForm.dirName);
      if (type == "look") {
        this.lookState = true;
      }
      if (!examId) {
        addExam(taskId).then((res) => {
          this.form.examId = res.data;
          this.exam_examId = res.data;
          this.exam_taskId = taskId;
          this.exam_paperOpen = true;
          this.exam_type = 1;
          this.isOfflineExam = false;
          this.$emit("refresh", res.data);
        });
      } else {
        this.exam_examId = examId;
        this.exam_taskId = taskId;
        this.exam_paperOpen = true;
        this.isOfflineExam = false;
        this.exam_type = examPublish == 0 ? 1 : 2;
      }
      console.log();
    },

    /* 添加作业 */
    handleAddHomework(practice, taskId, type) {
      localStorage.setItem("uuid", this.courseForm.dirName);
      let { practiceId, practicePublish } = practice;
      if (type == "look") {
        this.lookState = true;
      }
      if (!practiceId) {
        addHomework(taskId).then((res) => {
          this.form.homeworkId = res.data;
          this.exam_examId = res.data;
          this.exam_taskId = taskId;
          this.exam_paperOpen = true;
          this.isOfflineExam = false;
          this.exam_type = 7;
        });
      } else {
        this.exam_examId = practiceId;
        this.exam_taskId = taskId;
        this.exam_paperOpen = true;
        this.isOfflineExam = false;
        this.exam_type = practicePublish == 0 ? 7 : 8;
      }
    },
    /* 添加练习 */
    handleAddPractice(practice, taskId, type) {
      localStorage.setItem("uuid", this.courseForm.dirName);
      let { practiceId, practicePublish } = practice;
      if (type == "look") {
        this.lookState = true;
      }
      if (!practiceId) {
        addPractice(taskId).then((res) => {
          this.form.practiceId = res.data;
          this.exam_examId = res.data;
          this.exam_taskId = taskId;
          this.exam_paperOpen = true;
          this.isOfflineExam = false;
          this.exam_type = 4;
        });
      } else {
        this.exam_examId = practiceId;
        this.exam_taskId = taskId;
        this.exam_paperOpen = true;
        this.isOfflineExam = false;
        this.exam_type = practicePublish == 0 ? 3 : 4;
      }
    },
    /* 删除作业 */
    handleDelHomework(taskId) {
      this.$confirm(`确定删除作业吗?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        delHomework(taskId).then((res) => {
          this.form.homeworkId = null;
          this.form.homeworkPublish = 0;
          this.$emit("refresh", null);
          this.$modal.msgSuccess("删除成功");
        });
      });
    },
    /* 删除线上考核 */
    handleDelExam(taskId) {
      this.$confirm(`确定删除线上考核吗?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        delOnlineExam(taskId).then((res) => {
          this.form.examId = null;
          this.form.examPublish = 0;
          this.$emit("refresh", null);
          this.$modal.msgSuccess("删除成功");
        });
      });
    },
    /* 删除线下考核 */
    handleDelOfflineExam(taskId) {
      this.$confirm(`确定删除线下考核吗?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        delOfflineExam(taskId).then((res) => {
          this.form.offlineExamId = null;
          this.$emit("refresh", null);
          this.$modal.msgSuccess("删除成功");
        });
      });
    },
    /* 删除练习 */
    handleDelPractice(taskId) {
      this.$confirm(`确定删除练习吗?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        delPractice(taskId).then((res) => {
          this.form.practiceId = null;
          this.form.practicePublish = 0;
          this.$modal.msgSuccess("删除成功");
        });
      });
    },
    /* 添加考试返回 */
    changePaperOpen(e) {
      this.exam_paperOpen = false;
    },
  },
};

export default mixin;
